<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        :placeholder="$t('table.title')"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >{{ $t('table.search') }}</el-button>

      <router-link :to="'/article/create'">
        <el-button
          class="filter-item"
          style="margin-left: 10px;"
          type="success"
          icon="el-icon-circle-plus-outline"
        >
          {{
          $t('table.add')
          }}
        </el-button>
      </router-link>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      stripe
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column :label="$t('article.title')" min-width="120px">
        <template slot-scope="{ row }">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('article.author')" min-width="120px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.author }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('article.categoryName')" width="110px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.categoryName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('article.status')" class-name="status-col" width="80">
        <template slot-scope="{ row }">
          <el-tag :type="row.status === 0 ? 'warning' : 'success'">{{ row.status | statusFilter }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column :label="$t('article.gmtModified')" width="160px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.gmtModified | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')" align="center" width="160px">
        <template slot-scope="{ row, $index }">
          <!-- <el-button type="primary" size="mini" @click="handleUpdate(row)">{{ $t('table.edit') }}</el-button> -->
          <router-link :to="'/article/edit/'+row.id">
            <el-button type="primary" size="mini">{{ $t('table.edit') }}</el-button>
          </router-link>
          <el-button
            v-if="row.status != 'deleted'"
            size="mini"
            type="danger"
            @click="handleDelete(row, $index)"
          >{{ $t('table.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import MarkdownEditor from '@/components/MarkdownEditor'
import { fetchList, createArticle, updateArticle, deleteArticle } from '@/api/article'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
const statusOptions = [
  {
    value: 0,
    label: '草稿'
  },
  {
    value: 1,
    label: '发布'
  }
]
export default {
  name: 'LiveTable',
  components: { Pagination, Tinymce, MarkdownEditor },
  directives: { waves },
  filters: {
    statusFilter(status) {
      for (let i = 0; i < statusOptions.length; i++) {
        var item = statusOptions[i]
        if (status === item.value) {
          return item.label
        }
      }
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        title: ''
      },
      statusOptions: statusOptions,
      rules: {
        title: [{ required: true, message: 'title is required', trigger: 'blur' }],
        author: [{ required: true, message: 'author is required', trigger: 'blur' }],
        categoryName: [{ required: true, message: 'categoryName is required', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDelete(row, index) {
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      })
        .then(() => {
          deleteArticle({ ids: row.id })
            .then(response => {
              const { code } = response
              if (code === 1) {
                this.handleFilter()
                this.$notify({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
              } else {
                this.$notify({
                  title: '失败',
                  message: '删除失败',
                  type: 'error',
                  duration: 2000
                })
              }
            })
            .catch(error => {
              this.$notify({
                title: '异常',
                message: '删除数据出现异常',
                type: 'error',
                duration: 2000
              })
            })
        })
        .catch(() => {})
    }
  }
}
</script>
